package cn.blog.controller.front;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.addBean.BlogArtInfo;
import cn.blog.entity.addBean.PageBean;
import cn.blog.entity.origin.Article;
import cn.blog.entity.origin.ClassBean;
import cn.blog.entity.origin.Notice;
import cn.blog.entity.origin.User;
import cn.blog.service.front.BlogService;
import cn.blog.service.front.CompileHeadService;

@WebServlet("/front/blog")
public class BlogServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = (String) req.getSession().getAttribute("uname");
		
		String login = "登录";
		String loginUrl = "front/login";

		String reg = "注册";
		String regUrl = "front/register";
		if (uname != null && uname != "" && !uname.equals("登录")) {
			
			login = uname;
			loginUrl = "front/compile";
			reg = "退出";
			regUrl = "front/login";
		}
		req.setAttribute("login", login);
		req.setAttribute("reg", reg);
		req.setAttribute("loginUrl", loginUrl);
		req.setAttribute("regUrl", regUrl);
		req.getSession().setAttribute("url", "/front/blog");

		BlogService blog = new BlogService();
		// 模拟当前是第一页
		String currentPageStr = req.getParameter("currentPage");
		if (currentPageStr == null)
			currentPageStr = "1";
		int currentPage = Integer.parseInt(currentPageStr);
		// 认为每页显示12条
		int currentCount = 4;

		// 目的：就是想办法封装一个PageBean 并返回
		PageBean pageBean = new PageBean();
		int totalCount = blog.artCount();
		// 5、每页显示的数据
		int index = (currentPage - 1) * currentCount;

		// 查询所有文章类型
		List<ClassBean> cla = blog.classAll();

		req.setAttribute("cla", cla);

		String classID = req.getParameter("classId");

		// 查询所有文章
		List<Article> article;
		if (classID == "" || classID == null) {
			article = blog.artAll(index, currentCount);
		} else {
			int classId = Integer.parseInt(classID);

			article = blog.artAll(classId, index, currentCount);
			totalCount = article.size();

		}

		if (req.getSession().getAttribute("sousuo") != null) {
			if (req.getSession().getAttribute("sousuo").toString() != null
					&& !req.getSession().getAttribute("sousuo").toString().equals("")) {
				String artName = req.getSession().getAttribute("sousuo").toString();
				article = blog.artAll(artName, index, currentCount);
				req.getSession().setAttribute("sousuo", "");
				totalCount = article.size();
			}

		}
		if (article.size() == 0) {
			// 如果提交的是类型名则根据类型名查询
			if (req.getSession().getAttribute("classId") != null) {
				if (req.getSession().getAttribute("classId").toString() != null
						&& !req.getSession().getAttribute("classId").toString().equals("")) {
					int claId = Integer.parseInt(req.getSession().getAttribute("classId").toString());
					if (claId != 0) {
						article = blog.artAll(claId, index, currentCount);
						req.getSession().setAttribute("classId", "");
						totalCount = article.size();
						System.out.println(totalCount);
					}
				}
			}
		}

		List<BlogArtInfo> art = new ArrayList<>();
		for (int i = 0; i < article.size(); i++) {
			// 更具文章用户编号获取用户对象
			User user = blog.getUser(article.get(i).getArtUserId());
			// 更具文章编号获取评论数
			int comNum = blog.comNum(article.get(i).getArtId());
			BlogArtInfo text = new BlogArtInfo(article.get(i).getArtId(), article.get(i).getArtUserId(),
					article.get(i).getArtTitle(), article.get(i).getArtClassId(), article.get(i).getArtTime(),
					article.get(i).getArtRead(), article.get(i).getArtContext(), user.getName(), comNum);
			art.add(text);
		}
		// 1、当前页private int currentPage;
		pageBean.setCurrentPage(currentPage);
		// 2、当前页显示的条数private int currentCount;
		pageBean.setCurrentCount(currentCount);
		// 3、总条数private int totalCount;
		pageBean.setTotalCount(totalCount);
		// 4、总页数private int totalPage;
		int totalPage = (int) Math.ceil(1.0 * totalCount / currentCount);
		pageBean.setTotalPage(totalPage);

		pageBean.setDate(art);
		req.setAttribute("pageBean", pageBean);

		// 一周内的月底排行
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		Date date = new Date(System.currentTimeMillis());
		calendar.setTime(date);
		java.sql.Date endTime = new java.sql.Date(date.getTime());
		calendar.add(Calendar.WEEK_OF_YEAR, -1);
		date = calendar.getTime();
		java.sql.Date begTime = new java.sql.Date(date.getTime());
		List<Article> weekArt = blog.weekArt(begTime, endTime);

		req.setAttribute("weekArt", weekArt);

		// 获取所有通知
		List<Notice> not = blog.getAllNot();
		req.setAttribute("noti", not);

		forward("front/blog");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String sousuo = req.getParameter("sousuo");

		BlogService blog = new BlogService();
		int classId = 0;
		if (sousuo != null && sousuo != "") {
			classId = blog.getClaId(sousuo);
		}
		req.getSession().setAttribute("classId", classId);

		req.getSession().setAttribute("sousuo", sousuo);

		redirect("/front/blog");
	}

}
